package com.atguigu.gmall.gateway.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.cors.CorsConfiguration;
import org.springframework.web.cors.reactive.CorsWebFilter;
import org.springframework.web.cors.reactive.UrlBasedCorsConfigurationSource;

/*
 * @author MaShuaiHao
 * @date 2021/12/27 17:36
 */
@Configuration
public class CorsConfig {
    @Bean
    public CorsWebFilter corsWebFilter() {
        CorsConfiguration configuration = new CorsConfiguration();
        // *允许所有域名跨域访问: 不安全; 不能携带cookie
        configuration.addAllowedOrigin("http://manager.gmall.com");
        configuration.addAllowedOrigin("http://www.gmall.com");
        configuration.addAllowedOrigin("http://gmall.com");
        configuration.addAllowedOrigin("http://localhost:1000");
        //允许携带cookie
        configuration.setAllowCredentials(true);
        //允许携带所有头信息
        configuration.addAllowedHeader("*");
        //允许所有请求方式跨域访问
        configuration.addAllowedMethod("*");
        //给予url的跨域配置
        UrlBasedCorsConfigurationSource configurationSource = new UrlBasedCorsConfigurationSource();
        configurationSource.registerCorsConfiguration("/**" , configuration);
        return new CorsWebFilter(configurationSource);
    }
}
